package com.Crawlzongheng;

import java.util.HashMap;

import com.CrawlData.CrawlBase;
import com.Model.NovelInfoModel;
import com.Util.ParseMD5;
import com.Util.RegexUtil;
import com.Util.XmlUtil;
import com.Util.jsonUtil;
import com.zonghengdb.ZongHengDB;
/**
 * 简介页信息采集
 * @author john
 *
 */
public class IntroPage extends CrawlBase{
	private String url;
	//请求头信息
	private static HashMap<String, String> params;
	//书名正则
	private static final String NAME = "<meta property=\"og:title\" content=\"(.*?)\"/> ";
	//作者正则
	private static final String AUTHOR = "<meta name=\"og:novel:author\" content=\"(.*?)\"/>";
	//简介正则
	private static final String DESC = "<meta property=\"og:description\" content=\"(.*?)\"/> ";
	//分类正则
	private static final String TYPE = "<meta name=\"og:novel:category\" content=\"(.*?)\"/>";
	//最后章节正则
	private static final String LASTCHAPTER = "<a class=\"chap\" href=\".*?\">(.*?)<p>";
	//字数正则
	private static final String WORDCOUNT = "<span title=\".*?字\">(.*?)</span>字";
	//关键字HTML正则
	private static final String KEYWORDS = "<div class=\"keyword\">(.*?)</div>";
	//关键字正则
	private static final String KEYWORD = "<a.*?>(.*?)</a>";
	//章节URL正则
	private static final String CHAPTERLISTURL = "<meta name=\"og:novel:read_url\" content=\"(.*?)\"/>";
	static {
		params = new HashMap<String, String>();
		params.put("Referer", "http://book.zongheng.com");
		params.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36");
		params.put("Host", "book.zongheng.com");
	}
	//获取简介页信息
	public NovelInfoModel analyzer(){
		NovelInfoModel bean = new NovelInfoModel();
		bean.setUrl(url);
		bean.setId(ParseMD5.parseStrToMD5(bean.getUrl()));
		bean.setName(getName());
		bean.setAuthor(getAuthor());
		bean.setDesc(getDesc());
		bean.setType(getType());
		bean.setLastChapter(getLastChapter());
		bean.setWordCount(getWordCount());
		bean.setKeyWords(getKeyWords());
		bean.setChapterListUrl(getChapterListUrl());
		return bean;
	}
	
	
	public IntroPage(String url) {
		
		readPageByGet(url, params, "utf-8");
		
		this.url = url;
	}
	
	private String getName() {
		return RegexUtil.getFirstString(getPageSourceCode(), NAME, 1);
	}
	private String getAuthor(){
		return RegexUtil.getFirstString(getPageSourceCode(), AUTHOR, 1);
	}
	private String getDesc(){
		return RegexUtil.getFirstString(getPageSourceCode(), DESC, 1);
	}
	private String getType(){
		return RegexUtil.getFirstString(getPageSourceCode(), TYPE, 1);
	}
	private String getLastChapter(){
		return RegexUtil.getFirstString(getPageSourceCode(), LASTCHAPTER, 1);
	}
	private int getWordCount(){
		String wordCount = RegexUtil.getFirstString(getPageSourceCode(), WORDCOUNT, 1);
		return Integer.parseInt(wordCount);
	}
	private String getKeyWordStr(){
		return RegexUtil.getFirstString(getPageSourceCode(), KEYWORDS, 1);
	}
	private String getKeyWords(){
		return RegexUtil.getString(getKeyWordStr(),KEYWORD," ",1);
	}
	private String getChapterListUrl(){
		return RegexUtil.getFirstString(getPageSourceCode(), CHAPTERLISTURL, 1);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		IntroPage introPage = new IntroPage("http://book.zongheng.com/book/359388.html");
		System.out.println(introPage.getName());
		System.out.println(introPage.getAuthor());
		System.out.println(introPage.getDesc());
		System.out.println(introPage.getType());
		
		System.out.println(introPage.getLastChapter());
		
		System.out.println(introPage.getWordCount());
		System.out.println(introPage.getKeyWords());
		System.out.println(introPage.getChapterListUrl());
		ZongHengDB db=new ZongHengDB();
		db.updateNovelInfo(introPage.analyzer());
	}
	

}
